import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { getUserId, getUserInfo, logout, updateUserInfo } from '../api/request';
import { insertLog, listLog } from '../api/api-all';
import { ToastController } from '@ionic/angular';

@Component({
  selector: 'app-page-user',
  templateUrl: './page-user.component.html',
  styleUrls: ['./page-user.component.scss']
})
export class PageUserComponent implements OnInit {

  constructor(private router: Router, private toastController: ToastController) { }

  // 用户操作日志
  userLog: any

  // 修改用户信息
  open: boolean = false
  info: any = {
    level: "",
    sex: "",
    age: "",
  }

  ngOnInit(): void {
    const userInfo: any = getUserInfo()
    this.info = {
      level: userInfo.level,
      sex: userInfo.sex,
      age: userInfo.age,
    }
    this.listLog()
  }

  cancel() {
    this.open = false
  }

  async confirm() {
    const info = this.info
    updateUserInfo({
      level: info.level,
      sex: info.sex,
      age: Number(info.age),
    })
    this.open = false
    let toast = await this.toastController.create({
      message: '修改成功',
      duration: 1500,
      color: "success"
    })
    insertLog({userId: getUserId(),remark: "修改信息"}).then(res => {
      this.listLog()
    })
    await toast.present()
  }

  logout() {
    insertLog({ userId: getUserId(), remark: "用户下线" })
    logout()
    this.router.navigate(['/starnight-student/login'])
  }

  listLog() {
    listLog().then(res => {
      this.userLog = res
      console.log(this.userLog)
    })
  }

}
